package com.quick.controller.monitor;


import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.common.utils.poi.ExcelUtil;
import com.quick.framework.service.PasswordService;
import com.quick.system.domain.SysLoginInfo;
import com.quick.system.service.SysLoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 登录日志
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@RestController
@RequestMapping("/monitor/loginInfo")
public class SysLoginInfoController extends BaseController {
    @Autowired
    private SysLoginInfoService sysLoginInfoService;
    @Autowired
    private PasswordService passwordService;


    @PreAuthorize("@ps.hasPermission('monitor:loginInfo:list')")
    @GetMapping("/list")
    public ApiResponse list(SysLoginInfo loginInfo) {
        try {
            startPage();
            List<SysLoginInfo> list = sysLoginInfoService.selectSysLoginInfoList(loginInfo);
            return paging(list);
        } finally {
            clean();
        }
    }

    @Log(title = "登录日志", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ps.hasPermission('monitor:loginInfo:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfo loginInfo) {
        List<SysLoginInfo> list = sysLoginInfoService.selectSysLoginInfoList(loginInfo);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<SysLoginInfo>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ps.hasPermission('monitor:loginInfo:remove')")
    @Log(title = "登录日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{infoIds}")
    public ApiResponse remove(@PathVariable Long[] infoIds) {
        return success(sysLoginInfoService.deleteSysLoginInfoByInfoIds(infoIds));
    }

    @PreAuthorize("@ps.hasPermission('monitor:loginInfo:remove')")
    @Log(title = "登录日志", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/clean")
    public ApiResponse clean() {
        sysLoginInfoService.cleanLoginInfo();
        return success();
    }

    @PreAuthorize("@ps.hasPermission('monitor:loginInfo:unlock')")
    @Log(title = "账户解锁", businessType = BusinessTypeEnum.OTHER)
    @GetMapping("/unlock/{userName}")
    public ApiResponse unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginLockCache(userName);
        return success();
    }
}
